import 'package:flutter/material.dart';
import 'package:schoollifedemo/base/common_function.dart';
import 'package:schoollifedemo/base/url_contract.dart';
import 'package:schoollifedemo/commonwidget/circle_empty_widget.dart';
import 'package:schoollifedemo/commonwidget/common_base_widget.dart';
import 'package:schoollifedemo/commonwidget/text_widget_with_overflow.dart';
import 'package:schoollifedemo/entity/Entity.dart';

class CommonFriendCircleMessageWidget extends BaseStatefulWidget {
  final FriendCircleMessage? _friendCircleMessage;
  final OnButtonClick? onLoveClick;
  final OnButtonClick? onUnLoveClick;
  final OnButtonClick? onCommitClick;
  final OnButtonLongClickWithValue? onCommitItemClick;
  final OnButtonClick? onIconClick;

  final Color? defaultInsideColor;

  const CommonFriendCircleMessageWidget(
    this._friendCircleMessage, {
    Key? key,
    this.onCommitClick,
    this.onLoveClick,
    this.onUnLoveClick,
    this.onCommitItemClick,
    this.onIconClick,
    this.defaultInsideColor = const Color(0x66D0E7FF),
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _CommonFriendCircleMessageWidgetState();
  }
}

class _CommonFriendCircleMessageWidgetState
    extends BaseWidgetState<CommonFriendCircleMessageWidget> {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(left: 10, right: 10),
      child: CircleEmptyWidget(
        _messageBody(),
        width: MediaQuery.of(context).size.width,
        insideColor: widget.defaultInsideColor ?? const Color(0x66D0E7FF),
      ),
    );
  }

  Widget _messageBody() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        _say(
            accountIcon: _getIconAddress(
                widget._friendCircleMessage?.icon?.iconSrc ?? ''),
            accountName: widget._friendCircleMessage?.accountNickname ?? '',
            location: widget._friendCircleMessage?.location,
            isLove: widget._friendCircleMessage?.isLove ?? false,
            timeStamps: widget._friendCircleMessage?.sendTime ?? '',
            loveCount: widget._friendCircleMessage?.loveCount ?? 0,
            msg: widget._friendCircleMessage?.message ?? ''),
        _commit(
          commitList: widget._friendCircleMessage?.commitList,
          onButtonClick: widget.onCommitItemClick,
        ),
      ],
    );
  }

  Widget _say({
    required String accountName,
    String? location,
    required String msg,
    required String timeStamps,
    bool isLove = false,
    int loveCount = 0,
    required String accountIcon,
  }) {
    return Container(
      padding: const EdgeInsets.only(top: 15, bottom: 10, right: 5, left: 5),
      child: IntrinsicHeight(
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            SizedBox(
              width: 128,
              child: Column(
                children: [
                  GestureDetector(
                    onTap: () async {
                      if (widget.onIconClick != null) {
                        widget.onIconClick!();
                      }
                    },
                    child: ClipOval(
                      child: Image.network(
                        accountIcon,
                        fit: BoxFit.cover,
                        width: 60,
                        height: 60,
                      ),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 5),
                    child: TextWidgetWithOverflow(
                      accountName,
                      insideColor: Colors.transparent,
                      textColor: const Color(0xFF429CFF),
                      fontSize: 16,
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(),
                    child: TextWidgetWithOverflow(
                      _convertToDate(timeStamps),
                      insideColor: Colors.transparent,
                      fontSize: 12,
                    ),
                  ),
                  Offstage(
                    offstage: location?.isNotEmpty == true ? false : true,
                    child: Row(
                      children: [
                        Container(
                          margin: const EdgeInsets.only(left: 10),
                          width: 18,
                          height: 18,
                          child: Image.asset(
                            'assets/images/common/icon_location.png',
                          ),
                        ),
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.only(),
                            child: TextWidgetWithOverflow(
                              location ?? '',
                              textAlign: TextAlign.left,
                              insideColor: Colors.transparent,
                              textColor: Colors.black,
                              fontSize: 14,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: Column(
                children: [
                  Expanded(
                    child: TextWidgetWithOverflow(
                      msg,
                      insideColor: Colors.transparent,
                      maxLine: 8,
                      fontSize: 15,
                      textAlign: TextAlign.left,
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(right: 12),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        isLove
                            ? GestureDetector(
                                onTap: () async {
                                  if (widget.onUnLoveClick != null) {
                                    widget.onUnLoveClick!();
                                  }
                                },
                                child: SizedBox(
                                  width: 16,
                                  height: 16,
                                  child: Image.asset(
                                    'assets/images/common/icon_in_love.png',
                                  ),
                                ),
                              )
                            : GestureDetector(
                                onTap: () async {
                                  if (widget.onLoveClick != null) {
                                    widget.onLoveClick!();
                                  }
                                },
                                child: SizedBox(
                                  width: 18,
                                  height: 18,
                                  child: Image.asset(
                                    'assets/images/common/icon_not_in_love.png',
                                  ),
                                ),
                              ),
                        TextWidgetWithOverflow(
                          'love: $loveCount',
                          fontSize: 12,
                          insideColor: Colors.transparent,
                          maxLine: 1,
                          textAlign: TextAlign.start,
                          width: 80,
                        ),
                        GestureDetector(
                          onTap: () async {
                            if (widget.onCommitClick != null) {
                              widget.onCommitClick!();
                            }
                          },
                          child: SizedBox(
                            width: 16,
                            height: 16,
                            child: Image.asset(
                              'assets/images/common/icon_commit.png',
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _commit({
    List<FriendCircleCommit?>? commitList,
    OnButtonLongClickWithValue? onButtonClick,
  }) {
    return Container(
      margin: const EdgeInsets.only(bottom: 15, left: 15, right: 15),
      child: ListView.separated(
        physics: const NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        itemBuilder: (context, index) {
          FriendCircleCommit? commit = commitList?[index];
          return GestureDetector(
            onLongPress: () {
              if (onButtonClick != null) {
                onButtonClick(context, index);
              }
            },
            child: CircleEmptyWidget(
              _commitMsgWidget(
                commitNickname: commit?.commitUser?.nickname ?? '',
                commitMsg: commit?.commitMsg ?? '',
                commitIcon:
                    _getIconAddress(commit?.commitUser?.icon?.iconSrc ?? ''),
              ),
              height: 30,
              width: 10,
            ),
          );
        },
        separatorBuilder: (context, index) {
          return const SizedBox(
            height: 5,
          );
        },
        itemCount: commitList?.length ?? 0,
      ),
    );
  }

  Widget _commitMsgWidget({
    required String commitNickname,
    required String commitMsg,
    required String commitIcon,
  }) {
    return Row(
      children: [
        Container(
          width: 22,
          height: 22,
          margin: const EdgeInsets.only(left: 10),
          child: ClipOval(
            child: Image.network(
              commitIcon,
              fit: BoxFit.cover,
            ),
          ),
        ),
        Expanded(
          child: TextWidgetWithOverflow(
            '$commitNickname: $commitMsg',
            insideColor: Colors.transparent,
            textAlign: TextAlign.left,
            fontSize: 14,
          ),
        ),
      ],
    );
  }

  String _getIconAddress(String iconStr) {
    return UrlContract.baseAddress + '/' + iconStr;
  }

  String _convertToDate(String timeStamps) {
    final time = int.tryParse(timeStamps);
    if (time == null) {
      return '';
    }
    final dateTime = DateTime.fromMillisecondsSinceEpoch(time);
    return '${dateTime.year % 100}年${dateTime.month}月${dateTime.day}日 ${dateTime.hour}:${dateTime.minute}';
  }
}
